<template>
  <div style="width: 100%;">
    <div id="centreLeft3Chart" style="width:100%; height: 5.15rem;"></div>
  </div>
</template>

<script>
import echartMixins from '@/utils/resizeMixins'
import { createNamespacedHelpers } from 'vuex'
const { mapState } = createNamespacedHelpers('screen')

export default {
  data () {
    return {
      source: []
    }
  },
  computed: {
    ...mapState(['operatorScreenData'])
  },
  mixins: [echartMixins],
  watch: {
    'operatorScreenData.todaystationutilizationratesort': {
      deep: true,
      handler () {
        this.makeSource()
        this._chart.setOption({
          dataset: {
            source: _.cloneDeep(this.source)
          }
        })
      }
    }
  },
  mounted () {
    this.makeSource()
    this.draw()
  },
  methods: {
    makeSource () {
      const source = []
      _.forEach(this.operatorScreenData.todaystationutilizationratesort, (v, k) => {
        const item = []
        item.push(v.stationname, v.utilizationrate * 100)
        source.push(item)
      })
      this.source = source.reverse()
    },
    draw () {
      // 基于准备好的dom，初始化echarts实例
      this._chart = this.$echarts.init(document.getElementById('centreLeft3Chart'))
      //  ----------------------------------------------------------------
      const option = {
        title: {
          text: '',
          x: 'center',
          y: 0,
          textStyle: {
            color: '#B4B4B4',
            fontSize: 16,
            fontWeight: 'normal'
          }
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.1)',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#7B7DDC'
            }
          },
          formatter: (params) => {
            let res = '<div><p>'  + params[0].name + '</p></div>'
            const name = this.$t('s83wyEF5AeRqj4Spqv7ex')
            for (let i = 0; i < params.length; i++) {
              if (params[i].data != undefined) {
                res +=
                  '<p>' + params[i].marker + name + ':' + params[i].data[i+1].toFixed(2) + '%' + '</p>'
              }
            }
            return res
          }
        },
        legend: {
          show: false,
          textStyle: {
            color: '#B4B4B4'
          },
          top: '0%'
        },
        grid: {
          width: '80%',
          top: 30,
          bottom: 40
        },
        xAxis: {
          type: 'value',
          splitLine: { show: false },
          axisLine: {
            lineStyle: {
              color: '#B4B4B4'
            }
          },
          axisLabel: {
            formatter: '{value} %'
          }
        },
        yAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#B4B4B4'
            }
          },
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: this.$t('bt6bh5yGokZotKyrNHmB5'),
            type: 'bar',
            barWidth: 10,
            itemStyle: {
              normal: {
                barBorderRadius: 5,
                color: 'rgb(67,125,38)'
              }
            }
          }
        ],
        dataset: {
          source: _.cloneDeep(this.source)
        }
      }
      this._chart.setOption(option)
    }
  },
  destroyed () {
    window.onresize = null
  }
}
</script>

<style lang="scss" scoped>
</style>
